<?php

namespace IMT\EventBundle\Controller;

use Symfony\Component\Security\Core\Exception\AccessDeniedException;

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;


class DefaultController extends Controller
{
    public function deleteAction($id)
    {
    	 
    	// Récupération de l'event
    	$repository = $this->getDoctrine()->getRepository('IMTEventBundle:BaseEvent');
    	$event = $repository->findOneById($id);
    	 
    	if (!$event)
    		throw new NotFoundHttpException('L\'event ayant l\'id '.$id.' n\'existe pas');
    	 
    	$em = $this->getDoctrine()->getEntityManager();
    	$viewHandler = $this->get('fos_rest.view_handler');
    	 
    	// Vérification des droits d'acces
    	$securityContext = $this->get('security.context');
    	 
    	if(false === $securityContext->isGranted('VIEW', $event)){
    		throw new AccessDeniedException(403, 'Acces refuse pour '.$securityContext->getToken()->getUser()->getUsername());
    	}
    	
    	$em->remove($event);
    	$em->flush();
    	
    	return $this->redirect($this->generateUrl('accueil_planning'));
    }
}


